package www.rlicker.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import www.rlicker.model.GroupResource;
import www.rlicker.model.SysRole;
import www.rlicker.model.SysUser;
import www.rlicker.mybatis.Criteria;
import www.rlicker.service.IGroupResourceService;
import www.rlicker.service.ISysRoleService;
import www.rlicker.service.ISysUserService;
import www.rlicker.utils.BaseController;
import www.rlicker.utils.EncryptUtil;
import www.rlicker.utils.JsonResult;
import www.rlicker.utils.MessageConstant;
import www.rlicker.utils.StringUtil;

@Controller
@RequestMapping("/system")
public class LoginController extends BaseController{

	@Autowired
	private ISysUserService service;
	
	@Autowired
	private ISysRoleService sysRoleService;
	
	@Autowired
	private IGroupResourceService groupResourceService;
	
	@RequestMapping("/login")
	public String showList(){
		return "index";
	}
	
	@RequestMapping("/main")
	public String showWelcome(String userId, ModelMap map){
		SysRole role = sysRoleService.queryByRoleKey(userId);
		List<GroupResource> list = null;
		if (role != null) {
			Criteria<GroupResource> param = new Criteria<GroupResource>();
			param.addParam("resourceIdList", getResourceId(role.getResourceId()));
			param.addParam("parent_resource_id", 0);
			param.addParam("status", 0);
			list  = groupResourceService.queryPageList(param);
			list.forEach(f -> {
				Criteria<GroupResource> params = new Criteria<GroupResource>();
				params.addParam("resourceIdList", getResourceId(role.getResourceId()));
				params.addParam("parent_resource_id", f.getResourceId());
				params.addParam("status", 0);
				params.addParam("showType", 0);
	            List<GroupResource> lists =  groupResourceService.queryPageList(params);
	            f.setList(lists);
	            if (lists.size() > 0) {
					lists.forEach(s -> { 
						Criteria<GroupResource> paramse = new Criteria<GroupResource>();
						paramse.addParam("resourceIdList", getResourceId(role.getResourceId()));
						paramse.addParam("parent_resource_id", s.getResourceId());
						paramse.addParam("status", 0);
				        paramse.addParam("showType", 0);
			            List<GroupResource> listse =  groupResourceService.queryPageList(paramse);
			            s.setLists(listse);
					});
				}
	        });
		}
		map.put("userId", userId);
		map.put("list", list);
		return "main";
	}
	
	@RequestMapping(value="/userlogin" , method = { RequestMethod.POST })
	@ResponseBody
	public JsonResult userLogin(HttpServletRequest request) {
		JsonResult jsonResult = new JsonResult();
		String userId = request.getParameter("userId");
		String password = request.getParameter("password");
		if (StringUtil.isNullOrBlank(userId) || StringUtil.isNullOrBlank(password)) {
			jsonResult.declareFailure(MessageConstant.USER_TYPE_6);
			return jsonResult;
		}
		SysUser user = service.queryEntity(userId);
		if (user == null) {
			jsonResult.declareFailure(MessageConstant.USER_TYPE_7);
			return jsonResult;
		}
		password = EncryptUtil.encodeToken(password);
		if (!password.equals(user.getPassword())) {
			jsonResult.declareFailure(MessageConstant.USER_TYPE_8);
			return jsonResult;
		}
		if (user.getStatus() == RETURN_STRING) {
			jsonResult.declareFailure(MessageConstant.USER_TYPE_9);
			return jsonResult;
		}
		storeUserIntoSession(request,user);
		String content =user.getUserName()+"登录了系统";
		insertLog(content);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userId", user.getUserId());
		jsonResult.addAll(map);
		return jsonResult.declareSuccess();
	}
	
}
